package com.lby.graduation.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lby.graduation.entity.User;
import com.lby.graduation.mapper.UserMapper;
import com.lby.graduation.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** <p>
 * 前端控制器
 * </p>
 *
 * @author 李兵耀
 * @since 2020-11-30
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    /*注册用户*/
    @GetMapping("/register")
    public boolean register(User user) {
        return userService.save(user);
    }

    /*通过id查找用户*/
    @RequestMapping("/findUserById")
    public List findUserById(Integer id) {
        return userService.list(new QueryWrapper<User>().eq("user_id", id));

    }

    /*更新用户信息*/
    @GetMapping("/update")
    public boolean update(User user) {
        return userService.updateById(user);
    }

    @RequestMapping("/findUser")
    public List findUser(String username, String password) {
        return userService.list(new QueryWrapper<User>().eq("username", username).eq("password", password));
    }

    @GetMapping("/logicdelete")
    public void deleteById() {
        userService.removeById(2L);
    }

    /*admin后台*/
    @RequestMapping("/adminFindUser")
    public Map adminFindUser(Integer current, Integer size) {
        HashMap<String, Object> map = new HashMap<>();
        Page<User> userPage = new Page<>(current, size);
        List<User> list = userMapper.selectPageUser(userPage);
        long total = userPage.getTotal();
        map.put("code", 200);
        map.put("data", list);
        map.put("msg", "查找成功");
        map.put("count", total);
        return map;
    }

    /*通过id删除*/
    @RequestMapping("/deleteUser")
    public Map deleteUser(Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        boolean b = userService.removeById(userId);
        if (b = true) {
            map.put("code", 200);
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }

    /*批量删除*/
    @RequestMapping("/deleteUsers")
    public Map deleteUsers(String ids) {
        HashMap<String, Object> map = new HashMap<>();
        JSONArray array = JSON.parseArray(ids);//传过来的json字符串转换成json数组
        //List list = JSONArray.parseArray(array.toJSONString());
        List<Integer> idList = new ArrayList<>();
        for (int i = 0; i < array.size(); i++)//遍历将取出的值存到集合
        {
            String str = array.get(i).toString();
            idList.add(Integer.valueOf(str));
        }
        boolean b = userService.removeByIds(idList);
        if (b = true) {
            map.put("code", 200);
            map.put("msg", "删除成功");
            map.put("idList",idList);
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }


}
